﻿using System;
using System.Collections.Generic;
using System.Linq;
using Application.App;
using Domain.Types;
using DTO;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class TaskController : ControllerBase
    {
        public TaskApplication taskApplication { get; set; }
        public ILogger<UserController> _logger { get; set; }

        /// <summary>
        /// 创建任务  创建任务.第一步
        /// </summary>
        /// <param name="newTaskInput"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult NewTask(NewTaskInput newTaskInput)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.NewTask(int.Parse(uid), newTaskInput);

                return this.Success(new { TaskID = r.ID, TaskAuditID = r.TaskAudits.First().ID });
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }
        /// <summary>
        /// 修改时获取任务详情
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ModifyTaskDetailsOutput> GetModifyTaskDetails(int taskID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.GetModifyTaskDetails(int.Parse(uid), taskID);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 修改任务
        /// </summary>
        /// <param name="modifyTaskInput"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult ModifyTask(ModifyTaskInput modifyTaskInput)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.ModifyTask(int.Parse(uid), modifyTaskInput);

                return this.Success(new { TaskID = r.ID, TaskAuditID = r.TaskAudits.FirstOrDefault(i=>!i.IsAdd)?.ID });
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }

        }

        /// <summary>
        /// 用户钱包的钱支付任务   创建任务.支付
        /// </summary>
        /// <param name="TaskID">FromForm</param>
        /// <param name="TaskAuditID">FromForm</param>
        /// <returns>PaySuccess==false 需要冲值金额</returns>
        [HttpPost, TokenValidFilter]
        public ActionResult PayTask([FromForm] int TaskID, [FromForm] int TaskAuditID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.GetPayTaskChargeFee(int.Parse(uid), TaskID, TaskAuditID);

                if (r > 0)
                {
                    return this.Success(new { PaySuccess = false, PayMoney = r }, "余额不足");
                }

                var r1 = taskApplication.PayTask(int.Parse(uid), TaskID, TaskAuditID);

                return this.Success(new { PaySuccess = true, PayMoney = 0 }, "支付任务成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 用户删除未通过审核的任务
        /// </summary>
        /// <param name="TaskID"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult DelTaskByUser([FromForm] int TaskID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.DelTaskByUser(int.Parse(uid), TaskID);
               

                return this.Success(r, "删除成功！");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 用户追加任务 状态必须为待发布以为任务展示中，任务完成等状态
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult TaskAdditional([FromForm] int TaskID, [FromForm] int Num)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.TaskAdditional(int.Parse(uid), TaskID, Num);

                return this.Success((object)r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 更新状态  暂停中 = 20,任务关闭 = 32,任务恢复 = 21
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="taskStatus">暂停中 = 20,任务关闭 = 32,任务恢复 = 21</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult SetTaskStatus([FromForm] int TaskID,[FromForm] TaskStatus taskStatus)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.SetTaskStatus(int.Parse(uid), TaskID, taskStatus);


                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

    }
}
